package com.space.components.domain.core;


import com.space.components.domain.core.annotation.EnableEntityDiff;
import com.space.components.domain.core.diff.EntityDiff;
import com.space.components.domain.core.diff.EntityDiffUtils;
import org.springframework.util.SerializationUtils;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/5/29 6:52 下午
 */
@EnableEntityDiff
public abstract class Entity<ID extends Identifier<?>> implements Identifiable<ID> {
    private final Context context = new Context();

    protected void initSnapshot() {
        context.setSnapshot(SerializationUtils.deserialize(SerializationUtils.serialize(this)));
    }

    public Context getContext() {
        return context;
    }

    public <T> EntityDiff<T> diff() {
        return (EntityDiff<T>) EntityDiffUtils.diff(this, this.context.getSnapshot());
    }
}
